package com.sagereal.wtc.factorymode.Activity;

import android.content.Context;
import android.content.Intent;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.widget.Button;

import com.sagereal.wtc.factorymode.R;

public class FlashActivity extends BaseActivity {
    private Button mTestPass,mTestFail;
    private CameraManager mCameraManager;
    private String[] mCameraIds;
    private boolean mHasFlashlight = false; //设备是否支持闪光灯

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flash);
        setTitle(getString(R.string.title_flash_test));

        mTestPass = findViewById(R.id.test_pass);
        mTestFail = findViewById(R.id.test_fail);

        // 初始化CameraManager
        mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            mCameraIds = mCameraManager.getCameraIdList();
            mHasFlashlight = checkFlashlight();
        } catch (CameraAccessException e) {
            return;
        }
        openFlashlight();
        initView();
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            openFlashlight();
        } else {
            closeFlashlight();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeFlashlight();
    }

    @Override
    protected void onPause() {
        super.onPause();
        closeFlashlight();
    }

    @Override
    protected void onResume() {
        super.onResume();
        openFlashlight();
    }

    private void initView() {
        mTestPass.setOnClickListener(v -> {
            closeFlashlight();
            Intent intent = new Intent(FlashActivity.this, ItemTestActivity.class);
            intent.putExtra("ItemId", getIntent().getIntExtra("ItemId", -1)); // 获取启动该Activity时传递的ItemId
            intent.putExtra("status", "pass");
            intent.putExtra("check", getIntent().getStringExtra("check"));
            setResult(RESULT_OK , intent);
            finish();
        });
        mTestFail.setOnClickListener(v -> {
            closeFlashlight();
            Intent intent = new Intent(FlashActivity.this, ItemTestActivity.class);
            intent.putExtra("ItemId", getIntent().getIntExtra("ItemId", -1)); // 获取启动该Activity时传递的ItemId
            intent.putExtra("status", "fail");
            intent.putExtra("check", getIntent().getStringExtra("check"));
            setResult(RESULT_OK , intent);
            finish();
        });
    }

    private void closeFlashlight() {
        if(mHasFlashlight){
            setFlashlight(false);
        }
    }
    private void openFlashlight() {
        if(mHasFlashlight){
            setFlashlight(true);
        }
    }

    private void setFlashlight(boolean state) {
        if (mCameraIds == null) return;
        for (String cameraId : mCameraIds) {
            try {
                mCameraManager.setTorchMode(cameraId, state);
            } catch (CameraAccessException e) {
                return;
            }
        }
    }

    private boolean checkFlashlight() {
        if (mCameraIds == null) return false;
        for (String cameraId : mCameraIds) {
            try {
                CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraId);
                Boolean hasFlash = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
                if (hasFlash != null && hasFlash) {
                    return true;
                }
            } catch (CameraAccessException e) {
                return false;
            }
        }
        return false;
    }
}